package com.sc.nft.admin.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meta.push.plat.client.api.MetaPushPlatClient;
import com.sc.nft.admin.request.AddNoticeRequest;
import com.sc.nft.admin.request.FindOneNoticeRequest;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.UpdateNoticeInfoRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.NoticeInfoDao;
import com.sc.nft.entity.NoticeInfo;
import com.sc.nft.entity.SysMember;
import com.sc.nft.entity.vo.NoticeInfoVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.NoticeInfoService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 菜单表服务控制器
 *
 * @author rdg
 * @description
 * @since 2022-06-02
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/notice")
@Api(tags = "公告管理")
public class NoticeInfoAdminController {
    private final NoticeInfoService noticeInfoService;
    private final NoticeInfoDao noticeInfoDao;
    private final MetaPushPlatClient metaPushPlatClient;

    @PostMapping(value = "/listPagedNotice", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取公告列表", notes = "获取公告列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<NoticeInfoVO> listPagedNotice(@Valid @RequestBody PageRequest pageRequest) {
        Page<NoticeInfo> page = noticeInfoService.listPagedNotice(pageRequest.getPageNo(), pageRequest.getPageSize());
        return PageResult.success(page.convert(noticeInfo -> {
            NoticeInfoVO noticeInfoVO = new NoticeInfoVO();
            noticeInfoVO.setCreateTime(noticeInfo.getCreateTime());
            noticeInfoVO.setId(noticeInfo.getId());
            noticeInfoVO.setModifyTime(noticeInfo.getModifyTime());
            noticeInfoVO.setNoticeTitle(noticeInfo.getNoticeTitle());
            noticeInfoVO.setNoticeContent(noticeInfo.getNoticeContent());
            noticeInfoVO.setStartTime(noticeInfo.getStartTime());
            noticeInfoVO.setEndTime(noticeInfo.getEndTime());
            // 时间2024-04-02,这个type值被用来开始和结束时间控制他的属性类型,不在采用数据库.但是数据库字段依然保留,用来以后预防还会想要回来这个字段
            noticeInfoVO.setNoticeType(null);
            noticeInfoVO.setEnablePush(noticeInfo.getEnablePush());
            return noticeInfoVO;
        }));
    }

    @PostMapping(value = "/getOneById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看公告详情", notes = "查看公告详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<NoticeInfoVO> getOneById(@Valid @RequestBody FindOneNoticeRequest findOneNoticeRequest) {
        return Result.success(noticeInfoService.getOneById(findOneNoticeRequest.getId()));
    }

    @PostMapping(value = "/addNotice", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增公告", notes = "新增公告", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addNotice(@Valid @RequestBody AddNoticeRequest addNoticeRequest) {
        if(addNoticeRequest.getEnablePush()) {
            if(Objects.isNull(addNoticeRequest.getStartTime())
                || Objects.isNull(addNoticeRequest.getEndTime())) {
                throw new GlobalRunTimeException("开启推送时，上架时间不能为空");
            }
            if(DateTime.now().isAfter(addNoticeRequest.getStartTime())) {
                throw new GlobalRunTimeException("开始上架时间不能早于当前时间");
            }
            if(addNoticeRequest.getEndTime().before(addNoticeRequest.getStartTime())) {
                throw new GlobalRunTimeException("结束上架时间不能早于开始上架时间");
            }
        }
        SysMember sysMember = SysMemberPool.getMember();
        NoticeInfo noticeInfo = new NoticeInfo();
        BeanUtils.copyProperties(addNoticeRequest, noticeInfo);
        noticeInfo.setIsPushed(false);
        noticeInfo.setPushedTime(null);
        boolean insert = noticeInfo.insert();
        return Result.success(insert);
    }

    @PostMapping(value = "/updateNotice", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改公告", notes = "修改公告", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result updateNotice(@Valid @RequestBody UpdateNoticeInfoRequest updateNoticeInfoRequest) {
        if(updateNoticeInfoRequest.getEnablePush()) {
            if(Objects.isNull(updateNoticeInfoRequest.getStartTime())
                    || Objects.isNull(updateNoticeInfoRequest.getEndTime())) {
                throw new GlobalRunTimeException("开启推送时，上架时间不能为空");
            }
            if(DateTime.now().isAfter(updateNoticeInfoRequest.getStartTime())) {
                throw new GlobalRunTimeException("开始上架时间不能早于当前时间");
            }
            if(updateNoticeInfoRequest.getEndTime().before(updateNoticeInfoRequest.getStartTime())) {
                throw new GlobalRunTimeException("结束上架时间不能早于开始上架时间");
            }
        }
        NoticeInfo oldNoticeInfo = noticeInfoDao.getById(updateNoticeInfoRequest.getId());
        if(Objects.isNull(oldNoticeInfo)) {
            throw new GlobalRunTimeException("公告不存在");
        }
        NoticeInfo noticeInfo = new NoticeInfo();
        BeanUtils.copyProperties(updateNoticeInfoRequest, noticeInfo);
        noticeInfo.setIsPushed(oldNoticeInfo.getIsPushed());
        noticeInfo.setPushedTime(oldNoticeInfo.getPushedTime());
        return Result.success(noticeInfo.updateById());
    }

    @PostMapping(value = "/deleteNotice", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除公告", notes = "删除公告", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result deleteNotice(@Valid @RequestBody IdRequest idRequest) {
        new NoticeInfo().deleteById(idRequest.getId());
        return Result.success();
    }
}
